/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.microservice.system.controller;

import org.microservice.common.base.BaseControllerImpl;
import org.microservice.common.util.PageUtils;
import org.microservice.common.util.R;
import org.microservice.system.dao.ScheduleJobDao;
import org.microservice.system.entity.ScheduleJobEntity;
import org.microservice.system.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Map;

/**
 * 定时任务
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.2.0 2016-11-28
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleJobController extends BaseControllerImpl<ScheduleJobDao,ScheduleJobEntity> {
	@Autowired
	private ScheduleJobService scheduleJobService;
	
	/**
	 * 定时任务列表
	 */

	public R getListPage(@RequestParam Map<String, Object> params){
		PageUtils page = scheduleJobService.getPageList(params);
		int total = scheduleJobService.getPageListTotal(params);
		page.setTotalCount(total);
		return R.ok().put("data", page);
	}

	
	/**
	 * 保存定时任务
	 */

	public R save(@RequestBody ScheduleJobEntity scheduleJob){

		scheduleJobService.save(scheduleJob);
		
		return R.ok();
	}
	
	/**
	 * 修改定时任务
	 */

	public R update(@RequestBody ScheduleJobEntity scheduleJob){

		scheduleJobService.update(scheduleJob);
		
		return R.ok();
	}
	
	/**
	 * 删除定时任务
	 */

	public R deleteById(@RequestParam("id") Serializable jobId){
		scheduleJobService.delete(jobId);
		return R.ok();
	}
	
	/**
	 * 立即执行任务
	 */

	@GetMapping("/run")
	public R run(@RequestParam("id") long id){
		scheduleJobService.run(id);
		
		return R.ok();
	}
	
	/**
	 * 暂停定时任务
	 */

	@GetMapping("/pause")

	public R pause(@RequestParam("id") long id){
		scheduleJobService.pause(id);
		
		return R.ok();
	}
	
	/**
	 * 恢复定时任务
	 */

	@GetMapping("/resume")

	public R resume(@RequestParam("id") long id){
		scheduleJobService.resume(id);
		
		return R.ok();
	}

}
